<template>
    <div>
        <a-modal
            v-model:visible="visible"
            title="查看文件"
            @ok="handleOk"
            @cancel="visible = false"
            okText="完成"
            :confirmLoading="confirmLoading"
            width="900px"
            wrapClassName="recitingDiscipline-full-modal"
            :dialogStyle="{top:'0'}"
        >
            <div >
                <a-spin tip="正在加载中..." :spinning="lastMeetingSpinning">
                    <div style="font-weight: 600;color: #5ca3e5;margin-bottom: 5px;" >{{ currentData.fwjxsmc+currentData.fwjlx }}</div>
                    <div>
                        <a-button style="margin-right: 15px;" class="blue-btn" type="primary" @click="refreshFile(currentData)">刷新</a-button>
                        <a-button style="margin-right: 15px;" class="blue-btn" type="primary" @click="downloadFiles(currentData)">下载</a-button>
                        <a-button style="margin-right: 15px;" class="blue-btn" type="primary" v-if="currentData.fwjlx == '.pdf'" @click="reEdit(currentData)">重新编辑</a-button>
                        <a-button style="margin-right: 15px;" class="blue-btn" type="primary" v-if="currentData.fwjlx !== '.pdf'" @click="saveFiles(currentData)">保存</a-button>
                        <a-button style="margin-right: 15px;" class="blue-btn" type="primary" v-if="currentData.fwjlx !== '.pdf'" @click="generatePDFClick(currentData)">生成PDF</a-button>
                        <a-button style="margin-right: 15px;" class="green-l2-btn" type="primary" v-if="currentData.fwjlx !== '.pdf'" @click="generatePDFgzClick(currentData)">生成PDF并盖章</a-button>
                        <a-button style="margin-right: 15px;" class="green-l2-btn" type="primary" v-if="currentData.fwjlx == '.pdf'" @click="stampClick(currentData)">盖章</a-button>
                    </div>
                    <iframe 
                        v-if="programDataiframeSrc"
                        :key="iframeKey"
                        :src="programDataiframeSrc" 
                        ref="iframeServerEditingRef" 
                        scrolling="auto" 
                        width="100%"
                        :height="(windowInnerHeight - 205 )+'px'"
                        style="border: 0;"
                    ></iframe>
                    <div v-else style="height: calc(100vh - 200px);">
                        <a-empty description="暂无文件或文件发生错误！"/>
                    </div>
                </a-spin>
            </div>
            
        </a-modal>
    </div>
</template>
<script>
import { initWjfqqxtzWc, spcyjcqxtzMlList,webOfficeApiopenFile,spcySqsxWjjlgetLocalFileById,getDocxToPdf,againEditPdfOnlineFile,getDocxToPdfAndGz,pdfFileHcCa } from './service/index'
import YchConstant from "@/api/YchConstant";
import blobType from '@/common/blobType'
export default {
    components:{

    },
    props:{

    },
    data(){
        return{
            iframeKey:null,
            programDataiframeSrc:'',
            windowInnerHeight:window.innerHeight,
            visible:false,
            currentData:{},
            lastMeetingSpinning:false,
            printObj:{
                id:"viewFinalFile-print-ID", //需要打印的id名称
                popTitle:"符合性审评审核件",//文档的名称
                // preview:true,//预览功能，不好用
                zIndex:'20002',
                beforeOpenCallback(vue){
                    //console.log('打印回调',vue)
                },
                openCallback (vue) {//打开之前
                    //console.log('打开之前',vue)
                },
                closeCallback (vue) {//关闭了打印工具
                    //console.log('关闭了打印工具')
                },
            },
            confirmLoading:false,
        }
    },
    watch:{

    },
    mounted(){
        
    },
    methods:{
        getData(row){
            this.currentData = row
            this.lastMeetingSpinning = true
            this.getFileChange(row)
        },
        getFileChange(row){
            if(row.fwjlx == '.doc' || row.fwjlx == '.docx'){
                webOfficeApiopenFile({id:row.id}).then(res=>{
                    if(res.code == 200){
                        //console.log('我一直在请求吗？',res.result.wordUrl)
                        this.programDataiframeSrc = res.result.wordUrl
                    }else{
                        this.programDataiframeSrc = ''
                    }
                    this.lastMeetingSpinning = false
                })
            }else{
                this.programDataiframeSrc = bootConfig[process.env.NODE_ENV].locality_ApI + YchConstant.SPZX_SPCY + '/spcyRsWjmb/openWjjl?id='+row.id
                this.lastMeetingSpinning = false
            }
            this.iframeKey = Date.now()
        },
        refreshFile(){
            this.$emit('defectAdjustmentClick')
        },
        downloadFiles(val){
            var that = this
            
            var fileName = val.fwjxsmc?val.fwjxsmc:val.fwjccmc
            let param = { fywflcode:'001002',id:val.id,sqsxid:this.currentData.sqsxid}
            spcySqsxWjjlgetLocalFileById(param).then((data) => {
                if (!data) {
                    that.$message.warning('文件下载失败')
                return
                }
                    let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf(val.fwjlx)] }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + val.fwjlx)
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
            })
        },
        reEdit(val){
            this.confirmLoading = true
            againEditPdfOnlineFile({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                }
                this.confirmLoading = false
            })
        },
        saveFiles(val){
            this.confirmLoading = true
            getWebOfficeDownloadFile({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                }
                this.confirmLoading = false
            })
        },
        generatePDFClick(val){
            this.confirmLoading = true
            this.lastMeetingSpinning = true
            getDocxToPdf({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                    this.lastMeetingSpinning = false
                }
                this.confirmLoading = false
            })
        },
        generatePDFgzClick(val){
            this.confirmLoading = true
            this.lastMeetingSpinning = true
            getDocxToPdfAndGz({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                    this.lastMeetingSpinning = false
                }
                this.confirmLoading = false
            })
        },
        stampClick(val){//盖章
            this.confirmLoading = true
            this.lastMeetingSpinning = true
            pdfFileHcCa({id:val.id}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.refreshFile()
                }else{
                    this.$message.warning(res.message)
                    this.lastMeetingSpinning = false
                }
                this.confirmLoading = false
            })
        },
        handleOk(){
            this.confirmLoading = true
            initWjfqqxtzWc({sqsxid:this.currentData.sqsxid}).then(res=>{
                if(res.code == 200){
                    this.$message.success(res.message)
                    this.$emit('successViewFinalFile')
                    this.visible = false
                }else{
                    this.$message.warning(res.message)
                }
                this.confirmLoading = false
            })
        }
    }
}
</script>
<style lang="less">
.recitingDiscipline-full-modal{
    .ant-modal{
        padding: 0;
    }
    .ant-modal-body{
        padding: 8px
    }
}
.viewFinalFile-documents-table-box{
    .title-qymc-sxmc{
        text-align: center;
        font-family: 'SimSun' !important;
        font-size: 34px;
        font-weight: bold !important;
        padding: 6px;
        color: #000;
        border:1px dashed #d9d9d9;
        border-bottom: none;
    }
    .title-pdbh{
        border:1px dashed #d9d9d9;
        border-bottom: none;
        padding: 6px;
        text-align: right;
        .pdbh-title-text{
            font-size: 18px;
            font-weight: bold;
            font-family: '楷体_GB2312' !important;
        }
        .pdbh-right-text{
            text-align: center;
            color: #000;
            font-size: 16px;
        }
    }
    
    .th-text-box{
        font-size: 18px;
        // font-weight: 600;
        width: 55px;
        text-align: center;
        padding: 8px;
        font-family: '楷体_GB2312' !important;
        font-weight: bold;
        color: rgba(0, 0, 0, 0.65);
    }
    td{
        padding: 8px;
        text-align: center;
        font-size: 18px;
        font-family: '楷体_GB2312' !important;
        color: #000;
    }
}
@media print{
    #viewFinalFile-print-ID{
        .title-qymc-sxmc{
            text-align: center;
            font-family: 'SimSun' !important;
            font-size: 28px;
            font-weight: 600;
            padding: 6px;
            color: #000;
            border:1px dashed #d9d9d9;
            border-bottom: none;
        }
        .title-pdbh{
            border:1px dashed #d9d9d9;
            border-bottom: none;
            padding: 6px;
            text-align: right;
            .pdbh-title-text{
                font-size: 18px;
                font-weight: bold;
            }
            .pdbh-right-text{
                text-align: center;
                color: #000;
                font-size: 16px;
            }
        }
        
        .th-text-box{
            font-size: 18px;
            // font-weight: 600;
            width: 55px;
            text-align: center;
            padding: 8px;
            font-family: '楷体_GB2312' !important;
            font-weight: bold;
            color: rgba(0, 0, 0, 0.65);
        }
        td{
            padding: 8px;
            text-align: center;
            color: #000;
            font-size: 18px;
            font-family: '楷体_GB2312' !important;
        }
    }
}
</style>